//
//  HSClassRoomTableViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SVProgressHUD

class HSClassRoomTableViewController: HSBasicTableViewController {

    var dataArray:[HSClassRoomModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(HSClassRoomTableViewCell.self, forCellReuseIdentifier: "crCell")
        tableView.rowHeight = 68
        loadData()
    }

    override func loadData() {
        let url = "ZH-schoolname-S-getNewMessage"
        let key = userInfo["key"] as! String
        let lType = isPull ? 2 : 1
        let id = dataArray.last?.id ?? 0
        let param = ["key":key,"id":id,"load_type":lType] as [String : Any]
        refreshC.beginRefreshing()
        
        
        HSNetWorkTools.shared.downloadData(url: url, parameters: param) { [weak self] (data, error) in
            self?.refreshC.endRefreshing()
           
            guard let data = data as? NSDictionary else {
                SVProgressHUD.showInfo(withStatus: "网络请求错误！")
                return
            }
            
            switch data["result"] as! Int {
            case 0:
                let dictArr = data["data"] as! [[String:Any]]
                var modelArray = [HSClassRoomModel]()
                
                for dict in dictArr {
                    let model = HSClassRoomModel(dict: dict)
                    modelArray.append(model)
                   
                }
                
                if (self?.isPull)! {
                    self?.dataArray += modelArray
                } else {
                    self?.dataArray = modelArray
                }
                
                self?.tableView.reloadData()
            case 1:
                HSAlertView.bottomAlertView("已经没有更早的数据了")
            default:
                break
            }
             self?.isPull = false
        }
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "crCell", for: indexPath) as! HSClassRoomTableViewCell
        cell.model = dataArray[indexPath.row]
        return cell
    }

}
